package com.xinzhitong.www.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinzhitong.www.exception.*;
import com.xinzhitong.www.exception.comment.CommentInsertErrException;
import com.xinzhitong.www.exception.coursecommentreturn.NoCourseCommentException;
import com.xinzhitong.www.exception.coursegeneral.IdNoCourseException;
import com.xinzhitong.www.exception.courseintroduction.IdNoIntroductionException;
import com.xinzhitong.www.exception.coursemessage.NoCourseMessage;
import com.xinzhitong.www.exception.courseprice.NoPriceException;
import com.xinzhitong.www.exception.coursewatched.ClassIdErrException;
import com.xinzhitong.www.exception.coursewatched.CourseWatchedInsertErrException;
import com.xinzhitong.www.exception.coursewatchedselect.CourseWatchedNullException;
import com.xinzhitong.www.exception.video.NoVideoException;
import com.xinzhitong.www.pojo.Comment;
import com.xinzhitong.www.pojo.Course;
import com.xinzhitong.www.pojo.UserCommentCourse;
import com.xinzhitong.www.service.CourseService;
import com.xinzhitong.www.utils.Message;
import com.xinzhitong.www.utils.MyPageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 课程控制器
 *
 * @author 钟伟良 吴展鹏
 */
@CrossOrigin
@RestController
@RequestMapping("course")
public class CourseController {
    @Resource
    private CourseService courseService;

    /**
     * 根据课程id获取课程价格
     *
     * @param id id
     * @return 课程价格
     */
    @RequestMapping(value = "price", method = RequestMethod.GET)
    @ResponseBody
    public Message SelectPrice(int id) {
        try {
            return Message.createSuc(courseService.price(id));
        } catch (NoPriceException e) {
            e.printStackTrace();
            return Message.createErr("此课程不存在");
        }
    }

    /*
    * 添加一个课程信息(管理员限定）
     * @param Integer credentialId, 工种ID
     * @param String  courseName,   课程名称
     * @param String  courseIntro,  课程简介
     * @param Integer price,        课程价格
     * @param String  teacher,      课程老师
     * @param String  teacherIntro, 老师简介
     * @param String  forward,      课程面向人群
     * @param String  forPost       课程面向的岗位
     * @param MultipartFile picture 图片
    * @return 添加成功信息
    * */
    @ResponseBody
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "insertCourse",method = RequestMethod.PUT)
    public Message InsertCourse(
            @RequestParam Integer credentialId,
            @RequestParam String  courseName,
            @RequestParam(defaultValue = "", required = false) String  courseIntro,
            @RequestParam Integer price,
            @RequestParam String  teacher,
            @RequestParam(defaultValue = "", required = false) String  teacherIntro,
            @RequestParam(defaultValue = "", required = false) String  forward,
            @RequestParam(defaultValue = "", required = false) String  forPost,
            @RequestParam MultipartFile picture
    ){
        String msg = "添加成功";
        courseService.InsertACourse(
                credentialId,
                courseName,
                courseIntro,
                price,
                teacher,
                teacherIntro,
                forward,
                forPost,
                picture
        );
        return Message.createSuc(msg);
        //return Message.createSuc(msg);
    }

    /*
     * 编辑一个课程信息(管理员限定）
     * @param Integer id,           课程ID
     * @param Integer credentialId, 工种ID
     * @param String  courseName,   课程名称
     * @param String  courseIntro,  课程简介
     * @param Integer price,        课程价格
     * @param String  teacher,      课程老师
     * @param String  teacherIntro, 老师简介
     * @param String  forward,      课程面向人群
     * @param String  forPost       课程面向的岗位
     * @param MultipartFile picture 图片
     * @return 添加成功信息
     * */
    @ResponseBody
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "editCourse",method = RequestMethod.POST)
    public Message editCourse(
            @RequestParam Integer id,
            @RequestParam Integer credentialId,
            @RequestParam String  courseName,
            @RequestParam(defaultValue = "", required = false) String  courseIntro,
            @RequestParam Integer price,
            @RequestParam String  teacher,
            @RequestParam(defaultValue = "", required = false) String  teacherIntro,
            @RequestParam(defaultValue = "", required = false) String  forward,
            @RequestParam(defaultValue = "", required = false) String  forPost,
            @RequestParam MultipartFile picture
    ){
        String msg = "编辑成功";
        courseService.EditACourse(
                id,
                credentialId,
                courseName,
                courseIntro,
                price,
                teacher,
                teacherIntro,
                forward,
                forPost,
                picture
        );
        return Message.createSuc(msg);
        //return Message.createSuc(msg);
    }


    /*
    *
    * 删除课程(管理员限定）
    * @param Integer id 课程id
    * @return 添加成功信息
    * */
    @ResponseBody
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "deleteCourse",method = RequestMethod.DELETE)
    public Message DeleteCourse(
            @RequestParam List<Integer> idList
    ){
        String msg = "删除成功";
        courseService.deleteCourse(idList);
        //courseService.deleteCourse(id);
        return Message.createErr(msg);
    }

    /**
     * 获取课程概览 可以关键字模糊搜索
     *
     * @param word     关键字
     * @param name     工种名称  电工 焊工
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 课程信息
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "getCourseGeneral", method = RequestMethod.GET)
    public Message CourseGeneral(
            @RequestParam(value = "word", required = false, defaultValue = "") String word,
            @RequestParam(value = "name", required = false, defaultValue = "") String name,
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "8") Integer pageSize
    ) {
//        try {
//            return Message.createSuc(courseService.getCourseGeneral(name, word, pageNo, pageSize));
//        } catch (NoConsignGeneralException e) {
//            e.printStackTrace();
//            return Message.createErr("查询不到信息");
//        }
        return Message.createSuc(courseService.getCourseGeneral(name, word, pageNo, pageSize));
    }

    /*
    *
    * 获取单个课次详细信息
    * @param id 课程id
    * @return 单个课程详细信息
    * */
    @RequestMapping(value = "getACourseInfo",method = RequestMethod.GET)
    public Message ACourseInfo(@RequestParam ("id")Integer id){
        return Message.createSuc(courseService.getACourseInfo(id));
    }


    /**
     * 根据课时id获取课时视频
     *
     * @param id 课时id
     * @return 课时视频
     */
    @RequestMapping(value = "video", method = RequestMethod.GET)
    @ResponseBody
    public Message video(
            @RequestParam("id") Integer id
    ) {
        try {
            return Message.createSuc(courseService.video(id));
        } catch (NoVideoException e) {
            e.printStackTrace();
            return Message.createErr("课时暂无视频");
        }
    }

    /**
     * 根据课程id获取视频目录
     *
     * @param id 课程id
     * @return 视频目录
     */
    @RequestMapping(value = "catalogue", method = RequestMethod.GET)
    @ResponseBody
    public Message catalogue(@RequestParam(value = "id") Integer id) {
        return Message.createSuc(courseService.catalogue(id));
    }

    /**
     * 根据课程id获取课程简介
     *
     * @param id 课程id
     * @return 课程简介
     */
    @RequestMapping(value = "introduction", method = RequestMethod.GET)
    @ResponseBody
    public Message introduction(@RequestParam(value = "id") Integer id) {
        try {
            return Message.createSuc(courseService.introduction(id));
        } catch (IdNoIntroductionException e) {
            e.printStackTrace();
            return Message.createErr("此课程暂无简介");
        }
    }

    /**
     * 根据工种id获取课程概览
     *
     * @param id       id
     * @param pageNo   pageNo
     * @param pageSize pageSize
     * @return 课程概览
     */
    @RequestMapping(value = "general", method = RequestMethod.GET)
    @ResponseBody
    public Message general(
            @RequestParam(value = "id") Integer id,
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            PageHelper.startPage(pageNo, pageSize);
            List<Map<String, Object>> x = courseService.courseGeneral(id);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(x);
            MyPageInfo myPageInfo = new MyPageInfo(pageInfo);
            return Message.createSuc(myPageInfo);
        } catch (IdNoCourseException e) {
            e.printStackTrace();
            return Message.createErr("暂无更多");
        }
    }

    /**
     * 我的课程
     *
     * @return 我的课程
     */
    @RequestMapping(value = "courseMine", method = RequestMethod.GET)
    @ResponseBody
    public Message courseMine() {
        try {
            return Message.createSuc(courseService.courseMine());
        } catch (CourseMineNullException e) {
            e.printStackTrace();
            return Message.createErr("暂无购买课程");
        }
    }

    /**
     * 获取视频分享链接
     *
     * @param id 视频id
     * @return 链接
     */
    @RequestMapping(value = "videoLink", method = RequestMethod.GET)
    @ResponseBody
    public Message videoLink(@RequestParam(value = "id") Integer id) {
        try {
            return Message.createSuc(courseService.videoLink(id));
        } catch (CourseVideoLinkException e) {
            e.printStackTrace();
            return Message.createErr("此视频无法分享");
        }

    }

    /**
     * 插入评论
     *
     * @param userCommentCourse 评论信息
     * @return 插入是否成功
     */
    @RequestMapping(value = "courseComment", method = RequestMethod.PUT)
    @ResponseBody
    public Message courseComment(
            @RequestBody UserCommentCourse userCommentCourse
    ) {

        try {
            courseService.comment(userCommentCourse.getCourseId(), userCommentCourse.getText());
            return Message.createSuc("评论成功");
        } catch (CommentInsertErrException e) {
            e.printStackTrace();
            return Message.createErr("评论失败");
        }
    }

    /**
     * 显示课程评论
     *
     * @param id       课程id
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 评论详情
     */
    @RequestMapping(value = "commentReturn", method = RequestMethod.GET)
    @ResponseBody
    public Message commentReturn(
            @RequestParam(value = "id") Integer id,
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            PageHelper.startPage(pageNo, pageSize);
            List<Comment> x = courseService.returnComment(id);
            PageInfo<Comment> pageInfo = new PageInfo<>(x);
            MyPageInfo myPageInfo = new MyPageInfo(pageInfo);
            return Message.createSuc(myPageInfo);
        } catch (NoCourseCommentException e) {
            e.printStackTrace();
            return Message.createErr("此课程暂无评论");
        }

    }

    /**
     * 插入用户浏览课次
     *
     * @param classId 课时id
     * @return 插入是否成功
     * @throws CourseWatchedException 课程查看插入异常
     */
    @RequestMapping(value = "classWatched", method = RequestMethod.PUT)
    @ResponseBody
    public Message classWatched(@RequestParam(value = "classId") Integer classId) throws CourseWatchedException {
        try {
            courseService.watched(classId);
            return Message.createSuc("插入成功");
        } catch (CourseWatchedInsertErrException e) {
            e.printStackTrace();
            return Message.createErr("插入失败");
        } catch (ClassIdErrException e) {
            e.printStackTrace();
            return Message.createErr("课时号错误");
        }
    }

    /**
     * 获取用户在此课程的观看课时数
     *
     * @param courseId 课程号
     * @return 观看课时数
     * @throws CourseWatchedSelectException 课程观看数选择异常
     */
    @RequestMapping(value = "classWatchedReturn", method = RequestMethod.GET)
    @ResponseBody
    public Message classWatchedReturn(@RequestParam(value = "courseId") Integer courseId) throws
            CourseWatchedSelectException {
        try {
            return Message.createSuc(courseService.watchedReturn(courseId));
        } catch (CourseWatchedNullException e) {
            e.printStackTrace();
            return Message.createErr("用户在此课程没有浏览记录");
        }
    }

    /**
     * 获取系统课程消息详情
     *
     * @param id 课程id
     * @return 消息详情
     * @throws CourseMessageException 课程消息异常
     */
    @RequestMapping(value = "message", method = RequestMethod.GET)
    @ResponseBody
    public Message message(@RequestParam(value = "id") Integer id) throws CourseMessageException {
        try {
            return Message.createSuc(courseService.getMessage(id));
        } catch (NoCourseMessage e) {
            e.printStackTrace();
            return Message.createErr("暂无消息");
        }
    }

}
